package com.ymatou.iapi.godmch.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.addProviderBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.parameter.getProviderDetailBean;
import com.ymatou.iapi.godmch.parameter.getProviderListResp.provider;
import com.ymatou.iapi.godmch.service.addProviderCall;
import com.ymatou.iapi.godmch.service.getProviderDetailCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.country_info;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.serviceRelation;
import com.ymttest.database.sqlwapper.godmch.country_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.serviceProviderWapper;
import com.ymttest.database.sqlwapper.godmch.serviceRelationWapper;
import com.ymttest.utils.TestDataManager;
/**
*服务商详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getProviderDetail{
	private static getProviderDetailBean bean;
	private static getProviderDetailCall call;
	addProviderBean addbean=new addProviderBean();
	addProviderCall addcall=new   addProviderCall();
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ; 
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("服务商详情");
	}

	@Before
	public void caseUp() {
		bean=new getProviderDetailBean();
		call=new getProviderDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void check_verify() throws InterruptedException, JSONException
	{
		//验证
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
		//Provider 获取providerid
		HashMap hMap=new HashMap();
		hMap.put("providerId", bean.getProviderId());
		//mchTitle
		serviceProviderWapper sWapper=new serviceProviderWapper();
		Map pMap=sWapper.selectProviders(hMap).get(0);
		
		Logger.verifyEquals(Integer.parseInt(pMap.get("providerId").toString()), call.getProviderId(), "验证ProviderId");
		Logger.verifyEquals(pMap.get("mchTitle"), call.getName(), "验证Name");
		
		//Total
		serviceRelationWapper relationWapper=new serviceRelationWapper();
		Integer total=relationWapper.selectChannelNumByproviderId(bean.getProviderId());
		Logger.verifyEquals(total, call.getTotal(), "验证Total");
		//mchList
		if(total>0){
			JSONArray resp=call.getMchList();
			List<serviceRelation> relationlst =relationWapper.selectByProviderId(bean.getProviderId());
			Logger.comment(" #####  proivderlst ########## "+relationlst.size());
			for(int i=0;i<relationlst.size();i++)
			{
				serviceRelation relation=relationlst.get(i);
				HashMap mchMap=new HashMap();
				mchMap.put("mchId",relation.getMchId());
				
				mch_infoWapper mch_infoWapper=new mch_infoWapper();
				mch_info  mchinfo=(mch_info)mch_infoWapper.selectby(mchMap).get(0);
				JSONObject respObject=new org.json.JSONObject(resp.get(i).toString());
				
				Logger.verifyEquals(mchinfo.getMchId(), respObject.get("mchId"), "验证第"+(i+1)+"条mchId");
				Logger.verifyEquals(df.format(mchinfo.getCreateTime()), respObject.get("registerTime"), "验证第"+(i+1)+"条RegisterTime");
				Logger.verifyEquals(mchinfo.getMchTitle(),  respObject.get("mchTitle"), "验证第"+(i+1)+"条mchTitle");
				//查询代理国家
				country_infoWapper country_infoWapper=new country_infoWapper();
				country_info country=country_infoWapper.selectByPrimaryKey(mchinfo.getCountry().toString());
				Logger.verifyEquals(country.getCnTitle(), respObject.get("country").toString(), "验证第"+(i+1)+"条country");
			}
		}	
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_001() {
		Logger.start(true,"正例-服务商详情-有服务名称");
		try {
			
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			//修改服务商名称
			editmchBean eBean=GodmchManageCallService.IniteditmachBean(userId_channl);
			GodmchManageCallService.editmachCall(eBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			addbean.setProviderId(userId_channl);
			addbean.setServiceType(serviceType);
			addbean.setCountryList(countryList);
			addcall.setData(addbean);
			addcall.callService();		
			
			bean.setProviderId(userId_channl);
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_001_1() {
		Logger.start(true,"正例-服务商详情-没有服务商名称");
		try {
			
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			addbean.setProviderId(userId_channl);
			addbean.setServiceType(serviceType);
			addbean.setCountryList(countryList);
			addcall.setData(addbean);
			addcall.callService();		
			
			bean.setProviderId(userId_channl);
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_001_2() {
		Logger.start(true,"正例-服务商详情-服务商id与渠道商id一致");
		try {
			
			createmchBean createmchBean=GodmchManageCallService.InitAddProvider();
			int providerId1=createmchBean.getMchId();
			
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//修改服务商名称
			editmchBean editBean=GodmchManageCallService.IniteditmachBean(providerId1);
			editBean.setMchTitle("测试渠道商的所有服务商_"+TestDataManager.getRandomIntNum(10));
			GodmchManageCallService.editmachCall(editBean);
			
			GodmchManageCallService.InitAddServiceRelation(providerId1,mchBean.getMchId(),1);		
			
			bean.setProviderId(providerId1);
			bean.setMchId(mchBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_002() {
		Logger.start(true,"反例-服务商详情-服务商不存在");
		try {
			bean.setProviderId(9999);
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			Logger.verifyEquals(0, call.getTotal(), "验证Total");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_003() {
		Logger.start(true,"反例-服务商详情-服务商为空");
		try {
			//bean.setProviderId(9999);
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证msg");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_004() {
		Logger.start(true,"反例-服务商详情-渠道商名称不存在");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			//修改服务商名称
			editmchBean eBean=GodmchManageCallService.IniteditmachBean(userId_channl);
			GodmchManageCallService.editmachCall(eBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			addbean.setProviderId(userId_channl);
			addbean.setServiceType(serviceType);
			addbean.setCountryList(countryList);
			addcall.setData(addbean);
			addcall.callService();		
			
			bean.setProviderId(userId_channl);
			bean.setMchTitle("231231231");
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();			

			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			Logger.verifyEquals(0, call.getTotal(), "验证Total");	
			HashMap hMap=new HashMap();
			hMap.put("providerId", bean.getProviderId());
			//mchTitle
			serviceProviderWapper sWapper=new serviceProviderWapper();
			Map pMap=sWapper.selectProviders(hMap).get(0);
			
			Logger.verifyEquals(Integer.parseInt(pMap.get("providerId").toString()), call.getProviderId(), "验证ProviderId");
			Logger.verifyEquals(pMap.get("mchTitle"), call.getName(), "验证Name");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_005() {
		Logger.start(true,"反例-服务商详情-服务商与渠道商没有对应关系");
		try {
			
			createmchBean createmchBean=GodmchManageCallService.InitAddProvider();
			int providerId1=createmchBean.getMchId();
			
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//修改服务商名称
			editmchBean editBean=GodmchManageCallService.IniteditmachBean(providerId1);
			editBean.setMchTitle("测试渠道商的所有服务商_"+TestDataManager.getRandomIntNum(10));
			GodmchManageCallService.editmachCall(editBean);
			
			GodmchManageCallService.InitAddServiceRelation(providerId1,mchBean.getMchId(),1);		
			
			bean.setProviderId(providerId1);
			bean.setMchId(mchBean.getMchId()+1);
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();		

			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			Logger.verifyEquals(0, call.getTotal(), "验证Total");		
			HashMap hMap=new HashMap();
			hMap.put("providerId", bean.getProviderId());
			//mchTitle
			serviceProviderWapper sWapper=new serviceProviderWapper();
			Map pMap=sWapper.selectProviders(hMap).get(0);
			
			Logger.verifyEquals(Integer.parseInt(pMap.get("providerId").toString()), call.getProviderId(), "验证ProviderId");
			Logger.verifyEquals(pMap.get("mchTitle"), call.getName(), "验证Name");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_006() {
		Logger.start(true,"反例-服务商详情-渠道商与国家没有对应关系");
		try {
			createmchBean createmchBean=GodmchManageCallService.InitAddProvider();
			int providerId1=createmchBean.getMchId();
			
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//修改服务商名称
			editmchBean editBean=GodmchManageCallService.IniteditmachBean(providerId1);
			editBean.setMchTitle("测试渠道商的所有服务商_"+TestDataManager.getRandomIntNum(10));
			GodmchManageCallService.editmachCall(editBean);
			
			GodmchManageCallService.InitAddServiceRelation(providerId1,mchBean.getMchId(),1);		
			List<String> mchCountryList=new ArrayList();
			mchCountryList.add("NZL");
			bean.setProviderId(providerId1);
			bean.setMchCountryList(mchCountryList);
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();		

			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			Logger.verifyEquals(0, call.getTotal(), "验证Total");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_008() {
		Logger.start(true,"反例-服务商详情-当前页等于0");
		try {
			
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			//修改服务商名称
			editmchBean eBean=GodmchManageCallService.IniteditmachBean(userId_channl);
			GodmchManageCallService.editmachCall(eBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			addbean.setProviderId(userId_channl);
			addbean.setServiceType(serviceType);
			addbean.setCountryList(countryList);
			addcall.setData(addbean);
			addcall.callService();		
			
			bean.setProviderId(userId_channl);
			bean.setPageIndex(0);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_009() {
		Logger.start(true,"反例-服务商详情-每页显示数量等于0");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			//修改服务商名称
			editmchBean eBean=GodmchManageCallService.IniteditmachBean(userId_channl);
			GodmchManageCallService.editmachCall(eBean);
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			addbean.setProviderId(userId_channl);
			addbean.setServiceType(serviceType);
			addbean.setCountryList(countryList);
			addcall.setData(addbean);
			addcall.callService();		
			
			bean.setProviderId(userId_channl);
			bean.setPageIndex(1);
			bean.setPageSize(0);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证msg");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderDetail_010() {
		Logger.start(true,"反例-服务商详情-当前页大于总页数");
		try {
			
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			//修改服务商名称
			editmchBean eBean=GodmchManageCallService.IniteditmachBean(userId_channl);
			GodmchManageCallService.editmachCall(eBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			addbean.setProviderId(userId_channl);
			addbean.setServiceType(serviceType);
			addbean.setCountryList(countryList);
			addcall.setData(addbean);
			addcall.callService();		
			
			serviceRelationWapper relationWapper=new serviceRelationWapper();
			Integer total=relationWapper.selectChannelNumByproviderId(bean.getProviderId());
			
			bean.setProviderId(userId_channl);
			bean.setPageIndex((total/10)+1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			Logger.verifyEquals(0, call.getTotal(), "验证Total");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}